package com.momo.hotel.controller;

import com.momo.hotel.dao.pojo.TbComment;
import com.momo.hotel.dao.pojo.TbNews;
import com.momo.hotel.dao.pojo.TbUser;
import com.momo.hotel.dao.pojo.ext.TbCommentExt;
import com.momo.hotel.dao.pojo.ext.TbNewsExt;
import com.momo.hotel.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author qiang
 * @create 2019/7/5
 */
@Controller
public class CommentController {
    @Autowired
    CommentService commentService;

    /**
     * 添加评论
     *
     * @param request
     * @return
     */
    @RequestMapping("addComment")
    public void addComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        TbUser user = (TbUser) request.getSession().getAttribute("user");
        if (user != null) {
            //评论内容
            String comment_content = request.getParameter("comment_content");
            if (comment_content.trim().length() != 0) {
                //评论的用户id
                Integer userId = user.getId();
                //评论的新闻id
                TbNewsExt tbNewsExt = (TbNewsExt) request.getSession().getAttribute("tbNewsExt");
                Integer newsID = tbNewsExt.getId();
                //评论时间
                Date date = new Date();
                TbComment tbComment = new TbComment();
                tbComment.setUserid(userId);
                tbComment.setNewsid(newsID);
                tbComment.setCommentTime(date);
                tbComment.setCommentContent(comment_content);
                Integer integer = commentService.addComment(tbComment);
                if (integer != 0) {
                    response.getWriter().write("评论成功");
                } else {
                    response.getWriter().write("评论失败");
                }
            } else {
                response.getWriter().write("评论内容不能为空");
            }
        } else {
            response.getWriter().write("请登录");
        }

    }

    @ResponseBody
    @RequestMapping("selectComment")
    public Object selectComment(HttpServletRequest request) {
        TbNewsExt tbNewsExt = (TbNewsExt) request.getSession().getAttribute("tbNewsExt");
        Integer newsID = tbNewsExt.getId();
        List<TbCommentExt> tbCommentExts = commentService.selectComment(newsID);
        System.out.println(tbCommentExts+"######################");
        return tbCommentExts;
    }
}
